/**
 * 
 */
package com.smarthome.api.mobile.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.smarthome.common.mobile.Service;
import com.smarthome.common.tools.APIUtil;
import com.smarthome.core.housemanager.ResidenceManagerService;
import com.smarthome.core.propertystaffmanager.PropertyCompanyService;
import com.smarthome.domain.bean.ShPropertyCompany;
import com.smarthome.domain.bean.ShResidence;
import com.smarthome.domain.mobile.MobileCodeEnum;
import com.smarthome.domain.mobile.MobileResponse;
import com.smarthome.domain.mobile.req.MobilePropertyCompanyRequest;

/**
 * @author dhc
 * @date 2015年10月28日 下午11:03:43
 * @title  物业公司
 */
@org.springframework.stereotype.Service("mobile.property.company")
public class MobilePropertyCompanyService implements Service<MobileResponse<?>> {
	
	@Resource(name="residenceManagerService")
	private ResidenceManagerService residenceManagerService;
	
	@Resource(name="propertyCompanyService")
	private PropertyCompanyService companyService;
	
	/**
	 * @title 查询获得物业公司id
	 * @createTime 2015年10月28日 下午11:28:16
	 * @author dhc
	 */
	public void list(MobilePropertyCompanyRequest request, MobileResponse<Object> response){
		ShResidence qresidence = new ShResidence();
		qresidence.setId(request.getResidenceId());
		qresidence.setPropertyId(request.getCompanyId());
		List<ShResidence> residences = residenceManagerService.queryShResidences(qresidence);
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		if(null == residences) {
			response.setData(list);
			return ;
		}
		for(ShResidence residence : residences){
			Map<String,Object> map = new HashMap<String ,Object>();
			map.put("companyId", residence.getPropertyId() == null ? "" : residence.getPropertyId().toString());
			map.put("residenceId", residence.getId().toString());
			map.put("residenceName", residence.getPropertyName() == null ? "" :  residence.getPropertyName());
			map.put("workDate", residence.getPropertyWorktDate() == null ? "" : residence.getPropertyWorktDate());
			map.put("workAddr", residence.getPropertyAddr()==null ?"" : residence.getPropertyAddr());
			map.put("servicePhone", residence.getPropertyPhone()==null ? "" : residence.getPropertyPhone());
			if(null != residence.getPropertyId()){
				ShPropertyCompany company = companyService.queryShPropertyCompanyById(residence.getPropertyId());
				if(null != company){
					map.put("companyName", company.getName() == null ? "" : company.getName() ); 
					map.put("remark", company.getRemark()==null?"":company.getRemark());
					map.put("tel", company.getTel() == null ? ""  :  company.getTel());
					map.put("mobile", company.getMobile() ==null ? "" :  company.getMobile());
					map.put("address", company.getAddress() ==null ? "" :  company.getAddress());
				} else {
					map.put("companyName", ""); 
					map.put("tel", "");
					map.put("mobile","");
					map.put("remark", "");
					map.put("address", "");
				}
			}
			list.add(map);
		}
		
		response.setData(list);		
	}
	
	/**
	 * @title 获取当前用户id
	 * @createTime 2015年10月28日 下午11:25:17
	 * @author dhc
	 */
	public void current(MobilePropertyCompanyRequest request, MobileResponse<Object> response){
		if(!APIUtil.isLogin(request)){
			response.setMobileCodeObj(MobileCodeEnum.VISIT_RESTRICT);
			return;
		}
		Map<String,Object> map = new HashMap<String,Object>();
		ShResidence residence = residenceManagerService.queryResidenceById(request.getAccount().getResidenceId());
		if(null != residence){
			map.put("companyId", residence.getPropertyId() == null ? "" : residence.getPropertyId().toString());
			map.put("residenceId", residence.getId().toString());
			map.put("companyName", residence.getPropertyName() == null ? "" : residence.getPropertyName());
			map.put("residenceName", residence.getPropertyName() == null ? "" :  residence.getPropertyName());
			map.put("workDate", residence.getPropertyWorktDate() == null ? "" : residence.getPropertyWorktDate());
			map.put("workAddr", residence.getPropertyAddr()==null ?"" : residence.getPropertyAddr());
			map.put("servicePhone", residence.getPropertyPhone()== null ? "" : residence.getPropertyPhone());
			if(null != residence.getPropertyId()){
				ShPropertyCompany company = companyService.queryShPropertyCompanyById(residence.getPropertyId());
				if(null != company){
					map.put("remark", company.getRemark()==null?"":company.getRemark());
					map.put("tel", company.getTel() == null ? ""  :  company.getTel());
					map.put("mobile", company.getMobile() ==null ? "" :  company.getMobile());
					map.put("address", company.getAddress() ==null ? "" :  company.getAddress());
				} else {
					map.put("remark", "");
					map.put("tel", "");
					map.put("mobile","");
					map.put("remark", "");
					map.put("address", "");
				}
			}
		}else {
			map.put("companyId", "");
			map.put("residenceId", "");
			map.put("companyName", "");
			map.put("residenceName", "");
			map.put("remark", "");
			map.put("workDate", "");
			map.put("workAddr", "");
			map.put("servicePhone", "");
			map.put("phone", "");
			map.put("tel", "");
			map.put("mobile","");
		}
		response.setData(map);
	}
	
	

}
